﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MxWeiXinPF.Web.UI;
using MxWeiXinPF.Common;
using System.Web.UI.HtmlControls;

namespace MxWeiXinPF.Web.admin.users
{
    public partial class IndexTopPic_edit : ManagePage
    {
        private string action = MXEnums.ActionEnum.Add.ToString();
        protected MxWeiXinPF.Model.manager admin_info;
        protected Button btnSubmit;
        protected HtmlForm form1;
        private int id = 0;
        protected int shopid = 0;
        protected HtmlTextArea txtContent;
        protected TextBox txtImgUrl;
        protected TextBox txtLinkUrl;
        protected TextBox txtSortId;
        protected TextBox txtTitle;
        protected int wid = 0;

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (this.action == MXEnums.ActionEnum.Edit.ToString())
            {
                base.ChkAdminLevel("IndexTopPic_list", MXEnums.ActionEnum.Edit.ToString());
                if (!this.DoEdit(this.id))
                {
                    base.JscriptMsg("保存过程中发生错误啦！", "", "Error");
                }
                else
                {
                    base.JscriptMsg("修改信息成功！", "IndexTopPic_list.aspx?shopid=" + this.shopid, "Success");
                }
            }
            else
            {
                base.ChkAdminLevel("IndexTopPic_list", MXEnums.ActionEnum.Add.ToString());
                if (!this.DoAdd())
                {
                    base.JscriptMsg("保存过程中发生错误啦！", "", "Error");
                }
                else
                {
                    base.JscriptMsg("添加信息成功！", "IndexTopPic_list.aspx?shopid=" + this.shopid, "Success");
                }
            }
        }

        private bool DoAdd()
        {
            bool flag = false;
            MxWeiXinPF.Model.indextoppic model = new MxWeiXinPF.Model.indextoppic();
            MxWeiXinPF.BLL.indextoppic indextoppic2 = new MxWeiXinPF.BLL.indextoppic();
            model.shopid = this.shopid;
            model.title = this.txtTitle.Text.Trim();
            model.link_url = this.txtLinkUrl.Text.Trim();
            model.img_url = this.txtImgUrl.Text;
            if (this.wid > 0)
            {
                model.wid = new int?(this.wid);
            }
            else
            {
                MxWeiXinPF.Model.manager weiXinCode = base.GetAdminInfo();
                model.wid = new int?(weiXinCode.id);
            }
            if (this.shopid > 0)
            {
                model.shopid = this.shopid;
            }
            else
            {
                this.admin_info = base.GetAdminInfo();
                model.shopid = this.admin_info.id;
            }
            model.content = this.txtContent.Value;
            if (this.txtContent.Value.Trim().Length > 100)
            {
                model.zhaiyao = Utils.DropHTML(this.txtContent.Value, 100);
            }
            else
            {
                model.zhaiyao = Utils.DropHTML(this.txtContent.Value, this.txtContent.Value.Trim().Length);
            }
            model.sort_id = Utils.StrToInt(this.txtSortId.Text.Trim(), 0x63);
            if (indextoppic2.Add(model) > 0)
            {
                base.AddAdminLog(MXEnums.ActionEnum.Add.ToString(), "添加首页幻灯片:" + model.title);
                flag = true;
            }
            return flag;
        }

        private bool DoEdit(int _id)
        {
            bool flag = false;
            MxWeiXinPF.BLL.indextoppic indextoppic = new MxWeiXinPF.BLL.indextoppic();
            MxWeiXinPF.Model.indextoppic model = indextoppic.GetModel(_id);
            if (this.shopid > 0)
            {
                model.shopid = this.shopid;
            }
            else
            {
                this.admin_info = base.GetAdminInfo();
                model.shopid = this.admin_info.id;
            }
            if (this.wid > 0)
            {
                model.wid = new int?(this.wid);
            }
            else
            {
                MxWeiXinPF.Model.manager weiXinCode = base.GetAdminInfo();
                model.wid = new int?(weiXinCode.id);
            }
            model.title = this.txtTitle.Text.Trim();
            model.link_url = this.txtLinkUrl.Text.Trim();
            model.img_url = this.txtImgUrl.Text;
            model.content = this.txtContent.Value;
            if (this.txtContent.Value.Trim().Length > 100)
            {
                model.zhaiyao = Utils.DropHTML(this.txtContent.Value, 100);
            }
            else
            {
                model.zhaiyao = Utils.DropHTML(this.txtContent.Value, this.txtContent.Value.Trim().Length);
            }
            model.sort_id = Utils.StrToInt(this.txtSortId.Text.Trim(), 0x63);
            model.update_time = new DateTime?(DateTime.Now);
            if (indextoppic.Update(model))
            {
                base.AddAdminLog(MXEnums.ActionEnum.Edit.ToString(), "修改首页幻灯片:" + model.title);
                flag = true;
            }
            return flag;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            string queryString = MXRequest.GetQueryString("action");
            if (!string.IsNullOrEmpty(queryString) && (queryString == MXEnums.ActionEnum.Edit.ToString()))
            {
                this.action = MXEnums.ActionEnum.Edit.ToString();
                this.id = MXRequest.GetQueryInt("id");
                if (this.id == 0)
                {
                    base.JscriptMsg("传输参数不正确！", "back", "Error");
                    return;
                }
                if (!new MxWeiXinPF.BLL.indextoppic().Exists(this.id))
                {
                    base.JscriptMsg("记录不存在或已被删除！", "back", "Error");
                    return;
                }
            }
            if (!this.Page.IsPostBack)
            {
                base.ChkAdminLevel("IndexTopPic_list", MXEnums.ActionEnum.View.ToString());
                this.shopid = MXRequest.GetQueryInt("shopid");
                if (this.wid == 0)
                {
                    MxWeiXinPF.Model.manager weiXinCode = base.GetAdminInfo();
                    this.wid = weiXinCode.id;
                }
                if (this.shopid == 0)
                {
                    this.admin_info = base.GetAdminInfo();
                    this.shopid = this.admin_info.id;
                }
                if (this.action == MXEnums.ActionEnum.Edit.ToString())
                {
                    this.ShowInfo(this.id);
                }
            }
        }

        private void ShowInfo(int _id)
        {
            MxWeiXinPF.Model.indextoppic model = new MxWeiXinPF.BLL.indextoppic().GetModel(_id);
            this.txtTitle.Text = model.title;
            this.txtLinkUrl.Text = model.link_url;
            this.txtImgUrl.Text = model.img_url;
            this.txtContent.Value = model.content;
            this.txtSortId.Text = model.sort_id.ToString();
            if (model.shopid > 0)
            {
                this.shopid = model.shopid;
            }
            if (model.wid > 0)
            {
                this.wid = model.wid.Value;
            }
        }
    }
}